package com.cmccgz.udacs_landlord_webapp.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cmccgz.udacs_landlord_webapp.base.AppConfig;
import com.cmccgz.udacs_landlord_webapp.bean.WebConfig;
import com.cmccgz.udacs_landlord_webapp.interfaces.OnRvItemClickListenter;
import com.cmccgz.udacs_landlord_webapp.main.web.NWebActivity;
import com.cmccgz.udacs_landlord_webapp.main.web.WebActivity;
import com.cmccgz.udacs_landlord_webapp.utils.BannerGlideImageLoader;
import com.cmccgz.baselibrary.utils.ScreenUtils;
import com.cmccgz.baselibrary.utils.ToastUtils;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.bean.DeviceInfoBean;
import com.cmccgz.udacs_landlord_webapp.bean.HostMenuBean;
import com.cmccgz.udacs_landlord_webapp.bean.WebBean;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;

import java.util.List;


public class HostMenuAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<HostMenuBean> datas;
    private List<DeviceInfoBean> fastDatas;
    private List<WebBean> bannerDatas;
    private LayoutInflater inflater;
    private boolean isShowFoot = false;
    public static final int TYPE_BANNER = 1;
    public static final int TYPE_LABEL_FAST = 2;
    public static final int TYPE_FAST = 3;
    public static final int TYPE_MENU = 4;
    public static final int TYPE_FOOTER = 5;
    private Banner banner;
    private OnRvItemClickListenter listenter;
/*    public HostMenuAdapter(Context mContext, List<HostMenuBean> datas) {
        this.mContext = mContext;
        this.datas = datas;
        inflater = LayoutInflater.from(mContext);
    }*/

    public HostMenuAdapter(Context mContext, List<HostMenuBean> datas, List<DeviceInfoBean> fastDatas, List<WebBean> bannerDatas, OnRvItemClickListenter listenter) {
        this.mContext = mContext;
        this.datas = datas;
        this.fastDatas = fastDatas;
        this.bannerDatas = bannerDatas;
        inflater = LayoutInflater.from(mContext);
        this.listenter = listenter;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
     /*   if (viewType==0) {
            return new NormalViewHolder(inflater.inflate(R.layout.item_host_menu, parent, false));
        }else if (viewType==1){
            return new FooterViewHolder(inflater.inflate(R.layout.host_footer_view, parent, false));
        }*/
        switch (viewType) {
            case TYPE_BANNER:
                return new BannerViewHolder(inflater.inflate(R.layout.item_banner, parent, false));
            case TYPE_LABEL_FAST:
                return new FastLabelViewHolder(inflater.inflate(R.layout.item_fast_label, parent, false));
            case TYPE_FAST:
                return new FastViewHolder(inflater.inflate(R.layout.item_fast, parent, false));
            case TYPE_MENU:
                return new NormalViewHolder(inflater.inflate(R.layout.item_host_menu, parent, false));
            case TYPE_FOOTER:
                return new FooterViewHolder(inflater.inflate(R.layout.host_footer_view, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if (holder.itemView != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listenter != null) {
                        listenter.onItemClick(null, position, holder.itemView);
                    }
                }
            });
        }
        int itemViewType = getItemViewType(position);
        switch (itemViewType) {
            case TYPE_BANNER:
                bindBanner(holder);
                break;
            case TYPE_LABEL_FAST:

                break;
            case TYPE_FAST:
                bindFast(holder, position);
                break;
            case TYPE_MENU:
                bindNormal(holder, position);
                break;
            case TYPE_FOOTER:
                break;
        }

    }

    public void bindFast(RecyclerView.ViewHolder holder, int position) {
        FastViewHolder fastViewHolder = (FastViewHolder) holder;
        DeviceInfoBean deviceInfoBean = fastDatas.get(position - 2);
        GridLayoutManager.LayoutParams layoutParams = (GridLayoutManager.LayoutParams) fastViewHolder.itemView.getLayoutParams();
        layoutParams.setMargins(0, 0, 0, ScreenUtils.dpToPxInt(mContext, getFastDataSize() + 1 == position ? 12 : 1));
        fastViewHolder.itemView.setLayoutParams(layoutParams);
        fastViewHolder.tv_fast.setText(deviceInfoBean.house_name);
    }

    public void bindBanner(RecyclerView.ViewHolder holder) {
        final BannerViewHolder bannerViewHolder = (BannerViewHolder) holder;
        banner = bannerViewHolder.banner;
        banner.setImageLoader(BannerGlideImageLoader.instance());
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        banner.setImages(bannerDatas);
        banner.isAutoPlay(true);
        banner.setDelayTime(1500);
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                int realPosition = banner.toRealPosition(position);
                WebBean webBean = bannerDatas.get(realPosition);
                Intent intent = new Intent(mContext, WebActivity.class);
                intent.putExtra("webBean", webBean);
                mContext.startActivity(intent);
            }
        });
        banner.start();
    }

    public void bindNormal(RecyclerView.ViewHolder holder, int position) {
        final HostMenuBean hostMenuBean = datas.get(position - getFastDataSize() - 2);
        ((NormalViewHolder) holder).iv_menuIcon.setImageResource(hostMenuBean.getMenuIconRes());
        ((NormalViewHolder) holder).tv_menuName.setText(hostMenuBean.getMenuName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Class clazz = hostMenuBean.getClazz();
                if (clazz == HostMenuBean.EmptyMenu.class) {
                    return;
                }
                if (clazz != null) {
                    Intent intent = new Intent(mContext, NWebActivity.class);
                    switch (clazz.getSimpleName()) {
                        case "AccreditMngActivity"://授权管理
                            intent.putExtra(NWebActivity.KEY_CONFIG, WebConfig.get("授权管理", "http://183.232.65.75:8010/webapp/html/authorization.html?mobile=" + AppConfig.getMobile(mContext)));
                            break;
                        case "UnlockRecordActivity"://进出记录
                            intent.putExtra(NWebActivity.KEY_CONFIG, WebConfig.get("进出记录", "http://183.232.65.75:8010/webapp/html/inAndOutList.html?mobile=" + AppConfig.getMobile(mContext)));
                            break;
                        case "WarningActivity"://设备报障
                            intent.putExtra(NWebActivity.KEY_CONFIG, WebConfig.get("设备报障", "http://183.232.65.75:8010/webapp/html/warningList.html?mobile=" + AppConfig.getMobile(mContext)));
                            break;
                        case "HouseManagerActivity"://房屋管理
                            intent.putExtra(NWebActivity.KEY_CONFIG, WebConfig.get("房屋管理", "http://183.232.65.75:8010/webapp/html/tenantManage.html?mobile=" + AppConfig.getMobile(mContext)));
                            break;
                    }
                    mContext.startActivity(intent);
                } else
                    ToastUtils.show(mContext, "功能待开放！");
            }
        });
    }

    @Override
    public int getItemCount() {
        int baseSize = 1 + 1 + (getFastDataSize());
        if (isShowFoot)
            return (datas == null ? 1 : datas.size() + 1) + baseSize;
        else
            return (datas == null ? 1 : datas.size()) + baseSize;
    }

    private class NormalViewHolder extends RecyclerView.ViewHolder {

        private ImageView iv_menuIcon;
        private TextView tv_menuName;

        public NormalViewHolder(View itemView) {
            super(itemView);
            iv_menuIcon = ((ImageView) itemView.findViewById(R.id.iv_menuIcon));
            tv_menuName = ((TextView) itemView.findViewById(R.id.tv_menuName));
        }
    }

    private class FooterViewHolder extends RecyclerView.ViewHolder {

        public FooterViewHolder(View itemView) {
            super(itemView);
        }
    }

    private class BannerViewHolder extends RecyclerView.ViewHolder {
        private final Banner banner;

        public BannerViewHolder(View itemView) {
            super(itemView);
            banner = ((Banner) itemView.findViewById(R.id.banner));
        }
    }

    private class FastLabelViewHolder extends RecyclerView.ViewHolder {

        public FastLabelViewHolder(View itemView) {
            super(itemView);
        }
    }

    private class FastViewHolder extends RecyclerView.ViewHolder {

        private final TextView tv_fast;
        private final ImageView iv_fast;

        public FastViewHolder(View itemView) {
            super(itemView);
            tv_fast = ((TextView) itemView.findViewById(R.id.tv_fast));
            iv_fast = ((ImageView) itemView.findViewById(R.id.iv_fast));
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_BANNER;
        }
        if (position == 1) {
            return TYPE_LABEL_FAST;
        }
        int fastDataSize = getFastDataSize();
        if (position >= 2 && position < 2 + fastDataSize) {
            return TYPE_FAST;
        }
        if (datas != null && position < 2 + fastDataSize + datas.size()) {
            return TYPE_MENU;
        }
        if (position == getItemCount() - 1 && isShowFoot) {
            return TYPE_FOOTER;
        }
        return -1;
     /*   if (position==getItemCount()-1&&isShowFoot) {
            return 1;
        }else
            return 0;*/
    }

    public int getFastDataSize() {
        return fastDatas == null ? 0 : fastDatas.size();
    }

    public DeviceInfoBean getDeviceByPositioin(int position) {
        return getFastDataSize() == 0 ? null : fastDatas.get(position - 2);
    }

    public Banner getBanner() {
        return banner;
    }
}
